using Microsoft.AspNetCore.Mvc;
using Outpatient.ErrorCode;
using OutpatientApi.DTO;
using OutpatientApi.Methodimplementation.IServices;

namespace OutpatientApi.Controllers
{
    /// <summary>
    /// 病人挂号控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class PatientRegistrationController : ControllerBase
    {
        private readonly IPatientRegistrationService _patientRegistrationService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="patientRegistrationService">病人挂号服务</param>
        public PatientRegistrationController(IPatientRegistrationService patientRegistrationService)
        {
            _patientRegistrationService = patientRegistrationService;
        }

        /// <summary>
        /// 添加病人挂号
        /// </summary>
        /// <param name="addDto">添加病人挂号参数</param>
        /// <returns>添加结果</returns>
        [HttpPost]
        public async Task<IActionResult> AddPatientRegistration([FromBody] AddPatientRegistrationDto addDto)
        {
            try
            {
                var result = await _patientRegistrationService.AddPatientRegistrationAsync(addDto);
                return Ok(result);
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { msg = "服务器内部错误", error = ex.Message });
            }
        }

        /// <summary>
        /// 获取所有号类数据
        /// </summary>
        /// <returns>号类数据列表</returns>
        [HttpGet]
        public async Task<IActionResult> GetAllNumberCategories()
        {
            try
            {
                var result = await _patientRegistrationService.GetAllNumberCategoriesAsync();
                return Ok(result);
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { msg = "服务器内部错误", error = ex.Message });
            }
        }

        /// <summary>
        /// 获取所有科室信息
        /// </summary>
        /// <returns>科室信息列表</returns>
        [HttpGet]
        public async Task<IActionResult> GetAllDepartmentInfos()
        {
            try
            {
                var result = await _patientRegistrationService.GetAllDepartmentInfosAsync();
                return Ok(result);
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { msg = "服务器内部错误", error = ex.Message });
            }
        }


        /// <summary>
        /// 获取所有医生信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<GetDepartmentInfoDto>>> GetDepartmentInfo()
        {
            try
            {
                return await _patientRegistrationService.GetDepartmentInfo();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 查询病人挂号信息
        /// </summary>
        /// <param name="patientName">患者姓名（可选）</param>
        /// <param name="registerNo">挂号单号（可选）</param>
        /// <param name="idNumber">身份证号（可选）</param>
        /// <returns>病人挂号信息列表</returns>
        [HttpGet]
        public async Task<IActionResult> GetPatientRegistrations([FromQuery] string? patientName = null, [FromQuery] string? registerNo = null, [FromQuery] string? idNumber = null)
        {
            try
            {
                var result = await _patientRegistrationService.GetPatientRegistrationsAsync(patientName, registerNo, idNumber);
                return Ok(result);
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { msg = "服务器内部错误", error = ex.Message });
            }
        }

        /// <summary>
        /// 查询消费明细信息（多表连查）
        /// </summary>
        /// <param name="patientName">患者姓名（可选）</param>
        /// <param name="registerNo">挂号单号（可选）</param>
        /// <param name="drugName">药品名称（可选）</param>
        /// <param name="patientRegistrationId">病人挂号ID（可选）</param>
        /// <returns>消费明细信息列表</returns>
        [HttpGet]
        public async Task<IActionResult> GetConsumptionDetails([FromQuery] string? patientName = null, [FromQuery] string? registerNo = null, [FromQuery] string? drugName = null, [FromQuery] int? patientRegistrationId = null)
        {
            try
            {
                var result = await _patientRegistrationService.GetConsumptionDetailsAsync(patientName, registerNo, drugName, patientRegistrationId);
                return Ok(result);
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { msg = "服务器内部错误", error = ex.Message });
            }
        }

        /// <summary>
        /// 门诊结账
        /// </summary>
        /// <param name="checkoutDto">结账信息</param>
        /// <returns>结账结果</returns>
        [HttpPost]
        public async Task<IActionResult> Checkout([FromBody] CheckoutRequestDto checkoutDto)
        {
            try
            {
                var result = await _patientRegistrationService.CheckoutAsync(checkoutDto);
                return Ok(result);
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { msg = "服务器内部错误", error = ex.Message });
            }
        }
    }
} 